package com.xinya.wanandroidclient.ui

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.xinya.wanandroidclient.Constants
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.base.BaseFragment
import com.xinya.wanandroidclient.common.initFragment
import com.xinya.wanandroidclient.databinding.FragmentMainBinding
import com.xinya.wanandroidclient.ui.home.HomeFragment
import com.xinya.wanandroidclient.ui.me.MeFragment
import com.xinya.wanandroidclient.ui.square.SquareFragment
import com.xinya.wanandroidclient.ui.tab.TabFragment

/**
 *
 * @ClassName:      MainFragment$
 * @Description:    总的内容Fragment
 * @Author:         xinya
 * @CreateDate:     2021/2/23$ 19:39$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class MainFragment : BaseFragment() {

    companion object {
        private const val TAG = "MainFragment"
    }

    private val fragmentList = arrayListOf<Fragment>()

    // 首页
    private val homeFragment by lazy { HomeFragment() }

    // 项目 (项目与公众号的UI是一样的  所以此处用了同一个fragment 仅通过type进行区分)
    private val projectFragment by lazy {
        TabFragment().apply {
            arguments = Bundle().apply { putInt("type", Constants.PROJECT_TYPE) }
        }
    }

    // 广场
    private val squareFragment by lazy { SquareFragment() }

    // 微信公众号
    private val wxFragment by lazy {
        TabFragment().apply {
            arguments = Bundle().apply { putInt("type", Constants.WX_ACCOUNT_TYPE) }
        }
    }

    // 我的
    private val meFragment by lazy { MeFragment() }

    init {
        fragmentList.apply {
            add(homeFragment)
            add(projectFragment)
            add(squareFragment)
            add(wxFragment)
            add(meFragment)
        }
    }

    override fun getLayoutId() = R.layout.fragment_main

    private lateinit var dataBinding: FragmentMainBinding
    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentMainBinding
    }

    override fun init(savedInstanceState: Bundle?) {
        val vpHomeView = dataBinding.vpHome
        val btnNavView = dataBinding.btnNav

        vpHomeView.initFragment(this, fragmentList).apply {
            // 全部fragment 都进行缓存
            offscreenPageLimit = fragmentList.size
            // 禁止用户滑动
            isUserInputEnabled = false
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    // 因为 isUserInputEnabled = false 已经禁止用户滑动,所以检测页面变化就没有用到
                }
            })
        }

        btnNavView.apply {
            setOnNavigationItemSelectedListener {
                when (it.itemId) {
                    R.id.nav_home -> vpHomeView.setCurrentItem(0, false)
                    R.id.nav_project -> vpHomeView.setCurrentItem(1, false)
                    R.id.nav_navigation -> vpHomeView.setCurrentItem(2, false)
                    R.id.nav_wx -> vpHomeView.setCurrentItem(3, false)
                    R.id.nav_me -> vpHomeView.setCurrentItem(4, false)
                    else -> {

                    }
                }
                true
            }
        }

    }


}